import Qs from 'qs'
import { request } from "@/utils/request";
import type { IResponse, IAnswer } from ".";
import type { IDialogData, IPageResp, UserListParam, UserVO, LoginForm,ApplyForm, UpdatePassWordForm,UserInfo,InviteInfo, InviteLogin,JoinTeamType } from "./type";

const DIALOG_PREFIX = "/chat/copilotUser";
const PREFIX_CHAT_LOGIN = "/chat/copilotLogin"
const PREFIX_CHAT_APPLY = "/chat/copilotApply"
const PREFIX_CHAT_CUSTOMER = "/chat/copilotCustomer"
const PREFIX_CHAT_INVITE = "/chat/copilotInviteInfo"
export function addQuestion(data: IDialogData) {
	return request<IResponse<IAnswer>>({
		url: `${DIALOG_PREFIX}/addQuestion`,
		method: "post",
		data,
	});
}

export function saveOrUpdate(data: Partial<UserVO>) {
	return request({
		method: "post",
		url: `${DIALOG_PREFIX}/save-or-update`,
		data,
	});
}

export async function page(params: UserListParam) {
	return request<IPageResp<UserVO>>({
		method: "get",
		params,
		url: `${DIALOG_PREFIX}/page`,
	});
}

export function remove(id: string) {
	return request({
		method: "get",
		url: `${DIALOG_PREFIX}/remove/${id}`,
	});
}

export function getDetail(id: string) {
	return request<IResponse<UserVO>>({
		method: "get",
		url: `${DIALOG_PREFIX}/detail/${id}`,
	});
}

export function batchRemove(ids: string) {
	return request<IResponse<UserVO>>({
		method: "get",
		url: `${DIALOG_PREFIX}/batchRemove/${ids}`,
	});
}


export function updateSearchType(data: Pick<UserVO,'id'|'searchType'>) {
	return request<IResponse<UserVO>>({
		method: "post",
		url: `${DIALOG_PREFIX}/updateSearchType`,
		data
	});
}

// 登录获取验证码
export function getSmsCode(phone: string){
	return request<IResponse<UserVO>>({
		method: "get",
		url: `${PREFIX_CHAT_LOGIN}/getSmsCode?phone=${phone}`,
	});
}

// 前台登录
export function login(params:LoginForm){
	return request<IResponse<UserVO>>({
  method:'get',
	url: `${PREFIX_CHAT_LOGIN}/login`,
	params
})
}

// 申请试用
export function applyTrial(data:ApplyForm){
	return request<IResponse<UserVO>>({
    method:'post',
	url: `${PREFIX_CHAT_APPLY}/insert`,
	data
})
}

// 修改密码
export function updatePassword(params:UpdatePassWordForm){
	return request<IResponse<UserVO>>({
    method:'get',
	url: `${PREFIX_CHAT_LOGIN}/updatePassword`,
	params
})
}

// 获取用户信息
export function getUserInfo(){
	return request<IResponse<UserInfo>>({
    method:'get',
	url: `${PREFIX_CHAT_CUSTOMER}/info`,
})
}

// 检测token是否过期
export function apiCheckToken(){
	return request<IResponse<boolean>>({
    method:'get',
	url: `${PREFIX_CHAT_CUSTOMER}/checkToken`,
})
}

// 获取邀请的团队信息
export function apiGetInviteInfo(params:{inviteInfoId:string}){
	return request<IResponse<InviteInfo>>({
    method:'get',
	url: `${PREFIX_CHAT_INVITE}/info`,
	params
})
}

//邀请页登录
export function apiInviteLogin(params:InviteLogin){
	return request<IResponse<string>>({
    method:'get',
	url: `${PREFIX_CHAT_INVITE}/inviteLogin`,
	params
})
}

export function apiJoinTeam(params:JoinTeamType){
	return request<IResponse>({
    method:'get',
	url: `${PREFIX_CHAT_INVITE}/joinTeam`,
	params
})
}

// 编辑用户信息
export function updateUserInfo(data: { logoUrl?: string,name?: string }) {
	return request({
		method: "post",
		url: `${PREFIX_CHAT_CUSTOMER}/updateInfo`,
		data: Qs.stringify(data)
	});
}

// 获取用户团队列表
export function apiUserTeamList() {
	return request({
		method: "get",
		url: `/chat/copilotCustomer/listTeam`
	});
}

// 用户切换团队
export function apiChangeUserTeam(params: {teamId: string}) {
	return request({
		method: "get",
		url: `${PREFIX_CHAT_CUSTOMER}/changeTeam`,
    params
	});
}